package com.esri.core.map;

import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.io.handler.n;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.ServerFile;
import com.tencent.smtt.sdk.TbsVideoCacheTask;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class ServerFolder {
    protected long _totalFiles;
    private ArrayList<ServerFolder> a;
    private ArrayList<ServerFile> b;
    private String c;
    private String d;
    private int e;
    private ServerFolder f;
    private String g;
    private UserCredentials h;
    private boolean i;
    private ProgressListener j;
    private a k = new a();
    private boolean l;

    /* loaded from: classes.dex */
    public interface ProgressListener {
        void reportProgress(long j, long j2, String str, long j3, long j4);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public final class a implements ServerFile.ProgressListener {
        private long b;

        private a() {
        }

        public void a(long j) {
            this.b = j;
        }

        @Override // com.esri.core.map.ServerFile.ProgressListener
        public void reportProgress(String str, long j, long j2) {
            if (ServerFolder.this.j != null) {
                ServerFolder.this.j.reportProgress(this.b, ServerFolder.this._totalFiles, str, j, j2);
            }
        }
    }

    private ServerFolder(ServerFolder serverFolder) {
        this.e = serverFolder != null ? serverFolder.getFolderLevel() + 1 : 0;
        this.f = serverFolder;
    }

    public ServerFolder(String str) {
        this.c = str;
    }

    public static ServerFolder fromJson(JsonParser jsonParser, String str, UserCredentials userCredentials) throws JsonParseException, IOException {
        ServerFolder a2 = a(jsonParser, null, userCredentials);
        a2.c = str;
        return a2;
    }

    private static ServerFolder a(JsonParser jsonParser, ServerFolder serverFolder, UserCredentials userCredentials) throws JsonParseException, IOException {
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
            return null;
        }
        ServerFolder serverFolder2 = new ServerFolder(serverFolder);
        serverFolder2.setCredentials(userCredentials);
        b(jsonParser, serverFolder2, userCredentials);
        return serverFolder2;
    }

    public String getName() {
        return this.c;
    }

    public String getUrl() {
        return this.d;
    }

    public ServerFolder getParent() {
        return this.f;
    }

    public ArrayList<ServerFolder> getChildFolders() {
        if (this.a == null) {
            this.a = new ArrayList<>();
        }
        return this.a;
    }

    public ArrayList<ServerFile> getFiles() {
        if (this.b == null) {
            this.b = new ArrayList<>();
        }
        return this.b;
    }

    public boolean hasFiles() {
        ArrayList<ServerFile> arrayList = this.b;
        return (arrayList == null || arrayList.isEmpty()) ? false : true;
    }

    public int getFolderLevel() {
        return this.e;
    }

    public String getPath() {
        File file;
        if (this.g != null) {
            file = new File(this.g, this.c);
        } else if (this.f != null) {
            file = new File(this.f.getPath(), this.c);
        } else {
            file = new File(this.c);
        }
        return file.getPath();
    }

    public void setCredentials(UserCredentials userCredentials) {
        this.h = userCredentials;
    }

    public void setProgressListener(ProgressListener progressListener) {
        this.j = progressListener;
    }

    public void add(ServerFile serverFile) {
        getFiles().add(serverFile);
    }

    public void open(boolean z) throws EsriSecurityException, Exception {
        a(z, false, null, 0L, null);
    }

    public void download(String str) throws EsriSecurityException, Exception {
        this.g = str;
        this.i = false;
        c();
        a(true, true, str, 0L, this.k);
    }

    public ServerFile findFile(String str) throws EsriSecurityException, Exception {
        ServerFile serverFile;
        ArrayList<ServerFolder> arrayList;
        ArrayList<ServerFile> arrayList2 = this.b;
        if (arrayList2 != null) {
            Iterator<ServerFile> it = arrayList2.iterator();
            while (it.hasNext()) {
                serverFile = it.next();
                if (str.equals(serverFile.getName())) {
                    break;
                }
            }
        }
        serverFile = null;
        if (serverFile == null && (arrayList = this.a) != null) {
            Iterator<ServerFolder> it2 = arrayList.iterator();
            while (it2.hasNext()) {
                ServerFolder next = it2.next();
                next.a(false, false, getPath(), 0L, null);
                serverFile = next.findFile(str);
                if (serverFile != null) {
                    break;
                }
            }
        }
        return serverFile;
    }

    public ServerFolder findFolder(String str) throws EsriSecurityException, Exception {
        ArrayList<ServerFolder> arrayList = this.a;
        ServerFolder serverFolder = null;
        if (arrayList != null) {
            Iterator<ServerFolder> it = arrayList.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                ServerFolder next = it.next();
                if (next.c.equals(str)) {
                    serverFolder = next;
                    break;
                }
            }
            if (serverFolder == null) {
                Iterator<ServerFolder> it2 = this.a.iterator();
                while (it2.hasNext()) {
                    ServerFolder next2 = it2.next();
                    next2.a(false, false, getPath(), 0L, null);
                    serverFolder = next2.findFolder(str);
                    if (serverFolder != null) {
                        break;
                    }
                }
            }
        }
        return serverFolder;
    }

    public boolean moveFolder(ServerFolder serverFolder, ServerFolder serverFolder2) {
        ServerFolder serverFolder3;
        if (serverFolder2 == null) {
            serverFolder2 = this;
        }
        if (serverFolder == null || serverFolder2 == null || (serverFolder3 = serverFolder2.f) == this || serverFolder3 == null) {
            return false;
        }
        serverFolder.f.a.remove(serverFolder);
        serverFolder.f = serverFolder2;
        serverFolder2.a.add(serverFolder);
        return true;
    }

    public boolean deleteFolder(ServerFolder serverFolder) {
        ServerFolder serverFolder2;
        if (serverFolder == null || (serverFolder2 = serverFolder.f) == null) {
            return false;
        }
        serverFolder2.a.remove(serverFolder);
        return true;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        for (int i = 0; i < this.e; i++) {
            sb2.append("| ");
        }
        String sb3 = sb2.toString();
        sb.append(sb3);
        sb.append("|_");
        String str = this.c;
        if (str != null) {
            sb.append(str);
        } else {
            sb.append("<root>");
        }
        sb.append("\r");
        ArrayList<ServerFolder> arrayList = this.a;
        if (arrayList != null) {
            Iterator<ServerFolder> it = arrayList.iterator();
            while (it.hasNext()) {
                sb.append(it.next());
            }
        }
        ArrayList<ServerFile> arrayList2 = this.b;
        if (arrayList2 != null) {
            Iterator<ServerFile> it2 = arrayList2.iterator();
            while (it2.hasNext()) {
                sb.append(sb3);
                sb.append("| |_");
                sb.append(it2.next());
            }
        }
        return sb.toString();
    }

    private long a(boolean z, boolean z2, String str, long j, a aVar) throws EsriSecurityException, Exception {
        long j2;
        File file = new File(str, this.c);
        if (z2) {
            if (!file.isDirectory()) {
                file.mkdir();
            }
            ArrayList<ServerFile> arrayList = this.b;
            if (arrayList != null) {
                Iterator<ServerFile> it = arrayList.iterator();
                while (it.hasNext()) {
                    ServerFile next = it.next();
                    next.setProgressListener(aVar);
                    j++;
                    aVar.a(j);
                    next.download(file);
                }
            }
        }
        ArrayList<ServerFolder> arrayList2 = this.a;
        if (arrayList2 != null) {
            if (!this.i) {
                Iterator<ServerFolder> it2 = arrayList2.iterator();
                j2 = j;
                while (it2.hasNext()) {
                    ServerFolder next2 = it2.next();
                    JsonParser jsonParser = null;
                    try {
                        if (!next2.b()) {
                            jsonParser = h.a(next2.getUrl(), (Map<String, String>) null, n.a(next2.getUrl(), this.h));
                            b(jsonParser, next2, this.h);
                            next2.a();
                        }
                        if (z) {
                            j2 = next2.a(z, z2, file.getPath(), j2, aVar);
                        }
                        if (jsonParser != null) {
                            try {
                                jsonParser.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Throwable th) {
                        if (0 != 0) {
                            try {
                                jsonParser.close();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                        }
                        throw th;
                    }
                }
                this.i = true;
            } else if (!z) {
                return j;
            } else {
                Iterator<ServerFolder> it3 = arrayList2.iterator();
                j2 = j;
                while (it3.hasNext()) {
                    j2 = it3.next().a(z, z2, file.getPath(), j2, aVar);
                }
            }
            return j2;
        }
        return j;
    }

    private void a() {
        this.l = true;
    }

    private boolean b() {
        return this.l;
    }

    private static void b(JsonParser jsonParser, ServerFolder serverFolder, UserCredentials userCredentials) throws IOException, JsonParseException {
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if (currentName.equals("folders")) {
                c(jsonParser, serverFolder, userCredentials);
            } else if (currentName.equals("files")) {
                d(jsonParser, serverFolder, userCredentials);
            } else if (currentName.equals("name")) {
                serverFolder.c = jsonParser.getText();
            } else if (currentName.equals(TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL)) {
                serverFolder.d = jsonParser.getText();
            } else {
                jsonParser.skipChildren();
            }
        }
    }

    private static void c(JsonParser jsonParser, ServerFolder serverFolder, UserCredentials userCredentials) throws JsonParseException, IOException {
        ArrayList<ServerFolder> childFolders = serverFolder.getChildFolders();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            childFolders.add(a(jsonParser, serverFolder, userCredentials));
        }
    }

    private static void d(JsonParser jsonParser, ServerFolder serverFolder, UserCredentials userCredentials) throws JsonParseException, IOException {
        ArrayList<ServerFile> files = serverFolder.getFiles();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            ServerFile fromJson = ServerFile.fromJson(jsonParser, serverFolder, userCredentials);
            fromJson.setCredentials(userCredentials);
            files.add(fromJson);
        }
    }

    private void c() {
        if (this.j != null) {
            this._totalFiles = 0L;
            try {
                open(true);
                this._totalFiles = a(0L);
            } catch (Exception unused) {
            }
        }
    }

    private long a(long j) {
        long size = getFiles().size() + j;
        Iterator<ServerFolder> it = this.a.iterator();
        while (it.hasNext()) {
            size = it.next().a(size);
        }
        return size;
    }
}
